package com.qzdsoft.erpcloud.service.stock.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.qzdsoft.erpcloud.domain.stock.StockInboundDetail;
import com.qzdsoft.erpcloud.mapper.proj.MaterialQueryMapper;
import com.qzdsoft.erpcloud.mapper.stock.StockInboundDetailMapper;
import com.qzdsoft.erpcloud.mapper.stock.StockInboundDetailQueryMapper;
import com.qzdsoft.erpcloud.service.stock.StockInboundDetailService;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailEditInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailEditItemInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundDetailQueryInfo;
import com.qzdsoft.utils.StringUtil;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
@Service
public class StockInboundDetailServiceImpl implements StockInboundDetailService {
	private static final Logger logger = org.slf4j.LoggerFactory.getLogger(StockInboundDetailServiceImpl.class);
	@Autowired
	private StockInboundDetailQueryMapper stockInboundDetailQueryMapper;
	@Autowired
	private StockInboundDetailMapper stockInboundDetailMapper;
	@Autowired
	private MaterialQueryMapper materialMapper;
	
	@Override
	public List<StockInboundDetailInfo> selectDetailBy(StockInboundDetailQueryInfo info) {
		// TODO Auto-generated method stub
		return stockInboundDetailQueryMapper.selectById(info);
	}
	@Transactional
	@Override
	public Response<String> remove(List<Integer> ids) {
		int counts = stockInboundDetailMapper.deleteByIds(ids);
		if(counts!=ids.size()) {
		    logger.error("编辑采购入库订单明细错误，删除数据不对deletecount={},提交修改数据条数：{}",counts,ids.size());
            throw new RuntimeException("删除采购入库订单明细错误，更新数据不对deletecount="+counts);
        }
        return new Response<String>(ResultEnum.SUCCESS);
    }
	@Transactional
	@Override
	public Response<String> edit(StockInboundDetailEditInfo info) {
		//验证提交的数据
		List<StockInboundDetailEditItemInfo> details = info.getItems();
		if(CollectionUtils.isEmpty(details)) {
			logger.error("编辑采入库明细提交数据为空");
			return new Response<String>(ResultEnum.ERROR);
		}
		List<StockInboundDetail> addDetails = new ArrayList<StockInboundDetail>();//新增详情
		List<StockInboundDetail> editDetails = new ArrayList<StockInboundDetail>();//编辑详情
		for(StockInboundDetailEditItemInfo d:details) {
			StockInboundDetail detail = new StockInboundDetail();
			if(!StringUtil.isEmpty(d.getId())) {
				detail = stockInboundDetailMapper.selectByPrimaryKey(d.getId());
				if(detail == null) {
					logger.error("编辑采购入库单明细时,根据id,没找到相应的明细数据，可能该明细不存在",d.getId());
					throw new RuntimeException("编辑采购入库单明细时,根据id,没找到相应的明细数据，可能该明细不存在");
				}
				editDetails.add(detail);//修改
			}else {
				addDetails.add(detail);//新增
				detail.setBillNo(info.getBillno());//入库单据编号
				detail.setInboundId(info.getBillid());//入库单据id
				detail.setReceivableAccount(d.getReceivableAccount());//应收数量
			}
			MaterialInfo material = materialMapper.selectById(d.getMaterialId());
	        if(material==null) {
	            logger.error("物料id，根据物料id：{},没找到对应的物料信息");
	            throw new RuntimeException("物料id错误");
	        }
	        logger.debug("物料信息:id={},name={},code={}",material.getId(),material.getName(),material.getCode());
	        logger.debug("物料信息:id={},sname={},scode={}",material.getId(),material.getAliasName(),material.getAliasCode());
			//物料相关设置
	        detail.setMaterialId(d.getMaterialId());
	        detail.setMaterialCoding(material.getCode());
//	        detail.setcMaterialCoding(material.getCode());
	        detail.setMaterialName(material.getName());
	        detail.setMaterialSpec(material.getSpec());
	        detail.setMaterialTypeId(material.getTypeid());
	        detail.setBrandId(material.getBrandid());
	        detail.setMeasurementUId(material.getMearsureUintid());
	        detail.setcMaterialCoding(material.getAliasCode());
	        detail.setcMaterialName(material.getAliasName());
	        detail.setcMaterialSpec(material.getAliasSpec());
	        
	        detail.setStoreageId(d.getStoreageId());//入库仓id
	        detail.setPrice(d.getPrice());
	        detail.setIsGift(d.getIsGift()==0?false:true);
	        detail.setActualAccount(d.getActualAccount());
		}
		int count = 0;
		//新增
		 if(!CollectionUtils.isEmpty(addDetails)) {
			 count = stockInboundDetailMapper.insertList(addDetails);
			 if(count!=addDetails.size()) {
		            logger.error("编辑采购入库单明细错误，更新数据不对updatecount={},提交新增数据条数：{}",count,addDetails.size());
		            throw new RuntimeException("编辑采购入库单明细错误，更新数据不对updatecount="+count);
		        }
		 }
		 count=0;
		 //更新
		 if(!CollectionUtils.isEmpty(editDetails)) {
			 for(StockInboundDetail d:editDetails) {
				 count += stockInboundDetailMapper.updateByPrimaryKey(d);
			 }
			 if(count!=editDetails.size()) {
				 logger.error("编辑采购入库单明细错误，更新数据不对updatecount={},提交修改数据条数：{}",count,editDetails.size());
		            throw new RuntimeException("编辑采购入库单明细错误，更新数据不对updatecount="+count);
			 }
		 }
		return new Response<String>(ResultEnum.SUCCESS);
	}
	
	
	
}
